package com.rongshu.view.penpal.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.rongshu.R;
import com.rongshu.utils.DateUtils;
import com.rongshu.utils.GlideUtils;
import com.rongshu.view.base.AdapterListener;
import com.rongshu.view.penpal.bean.PenpalRecommendArticleResponse;
import com.utils.FastClickUtils;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.List;

/**
 * created by zhengweis on 2018/11/1.
 * description：笔友会首页的文章列表
 */
public class PenpalRecommendArticleAdapter extends RecyclerView.Adapter<PenpalRecommendArticleAdapter.ViewHolder> {
    private Context context;
    private List<PenpalRecommendArticleResponse.ResponseBean> list;
    private AdapterListener listener;
    public PenpalRecommendArticleAdapter(Context context, List<PenpalRecommendArticleResponse.ResponseBean> list) {
        this.list = list;
        this.context = context;
    }

    public void setAdapterListener(AdapterListener listener){
        this.listener = listener;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(context).inflate(R.layout.adapter_penpal_recommend_article_item, viewGroup, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder, final int i) {
        viewHolder.postCover.setVisibility(TextUtils.isEmpty(list.get(i).getTopImg()) ? View.GONE : View.VISIBLE);
        GlideUtils.loadImageView(context, viewHolder.postCover, list.get(i).getTopImg(),
                R.drawable.default_preview_icon, R.drawable.default_preview_icon);
        viewHolder.title.setVisibility(TextUtils.isEmpty(list.get(i).getTitle()) ? View.GONE : View.VISIBLE);
        viewHolder.title.setText(list.get(i).getTitle());
        try {
            String content = list.get(i).getContents_header().replaceAll("%(?![0-9a-fA-F]{2})", "%25");
            viewHolder.content.setText(URLDecoder.decode(content, "utf-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        viewHolder.name.setText(list.get(i).getBi_name());
        viewHolder.time.setText(DateUtils.ConvertDate(list.get(i).getCreate_datetime()));
        viewHolder.vip.setVisibility(list.get(i).getVip() ? View.VISIBLE : View.GONE);
        GlideUtils.loadImageView(context, viewHolder.logo, list.get(i).getAvatar(),
                R.drawable.user_head_icon, R.drawable.user_head_icon);
        //分享标签
        if (TextUtils.equals(list.get(i).getTopic_type(), "transpond")) {
            viewHolder.shareLabel.setVisibility(View.VISIBLE);
            viewHolder.shareLabel.setImageResource(R.drawable.article_flow_share_icon);
        } else {
            viewHolder.shareLabel.setVisibility(View.GONE);
        }
        //精华、置顶标签
        if (list.get(i).getTopic_state() == 1) {
            viewHolder.bottomLabel.setVisibility(View.VISIBLE);
            viewHolder.bottomLabel.setImageResource(R.drawable.top_icon);
        } else if (list.get(i).getTopic_state2() == 1) {
            viewHolder.bottomLabel.setVisibility(View.VISIBLE);
            viewHolder.bottomLabel.setImageResource(R.drawable.essence_icon);
        } else {
            viewHolder.bottomLabel.setVisibility(View.GONE);
        }
        if (list.get(i).getContribute_id() == 0) {
            viewHolder.articleType.setText("帖子");
        } else {
            viewHolder.articleType.setText("文章");
        }
        viewHolder.comment.setText("评论 " + list.get(i).getCommentCount());
        viewHolder.praise.setText("点赞 " + list.get(i).getLikeCount());
        viewHolder.line.setVisibility(i == list.size() - 1 ? View.GONE : View.VISIBLE);
        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (listener != null && !FastClickUtils.isFastClick()){
                    listener.callBack(i);
                }
            }
        });

    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private TextView name, time, comment, praise, articleType, content, title;
        private ImageView postCover, logo, bottomLabel, shareLabel, vip;
        private View line;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            postCover = itemView.findViewById(R.id.post_cover);
            logo = itemView.findViewById(R.id.author_logo);
            name = itemView.findViewById(R.id.author_name);
            time = itemView.findViewById(R.id.wirte_time);
            articleType = itemView.findViewById(R.id.article_type);
            bottomLabel = itemView.findViewById(R.id.bottom_label);
            shareLabel = itemView.findViewById(R.id.share_label);
            comment = itemView.findViewById(R.id.bottom_comment);
            praise = itemView.findViewById(R.id.bottom_praise);
            title = itemView.findViewById(R.id.article_title);
            content = itemView.findViewById(R.id.content);
            line = itemView.findViewById(R.id.line);
            vip = itemView.findViewById(R.id.vip);
        }
    }
}
